import express from 'express';
import session from 'express-session';
import webpack from 'webpack';
import bodyParser from 'body-parser';
import uuid from 'node-uuid';
import config from '../../webpack.config';

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(session({
  name: 'eduAdmin',
  secret: 'sjhkv343jkkj@jjk',
  cookie: ({ maxAge: 120 * 60000 }),
  resave: true,
  saveUninitialized: false
}));

if (process.env.NODE_ENV !== 'production') {
  // webpack compile
  const compiler = webpack(config);
  const options = {
    publicPath: config.output.publicPath,
    noInfo: true,
    stats: { colors: true },
  };
  app.use(require('webpack-dev-middleware')(compiler, options));
  app.use(require('webpack-hot-middleware')(compiler));
  //app.use(require('./mockMiddleware'));
}


app.use(require('./apiMiddleware'));
app.use(require('./ssrMiddleware'));

app.disable('x-powered-by');

const server = app.listen(8080, () => {
  const { port } = server.address();
  console.info(`Listened at http://localhost:${port}`);
});